<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="dialogType==='edit'?'编辑页面':'新增页面'"
    :close-on-click-modal="false"
  >
    <el-form :model="info" label-width="80px" label-position="left">
      <el-form-item label="页面名称">
        <el-input v-model="info.name" />
      </el-form-item>
      <el-form-item label="插件名称">
        <el-input v-model="info.code" :disabled="!isNew" />
      </el-form-item>
      <el-form-item v-if="isNew" label="上传组件">
        <el-upload
          ref="upload"
          class="upload-demo"
          drag
          action
          :http-request="uploadFileMethod"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          :auto-upload="false"
          :accept="'application/zip'"
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击选择</em>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="插件名称">
        <el-select v-model="info.status">
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div style="text-align:right;">
      <el-button type="danger" @click="cancel">取消</el-button>
      <el-button type="primary" @click="submitUpload">添加</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPage, editPage } from "@/api/page";

export default {
  props: {
    dialogVisible: Boolean,
    dialogType: {
      type: String,
      default: "New"
    },
    pageInfo: {
      type: Object,
      default: function() {
        return { name: "", code: "", fileId: "", status: 1 };
      }
    }
  },
  data() {
    return {
      statusList: [
        {
          label: "启用",
          value: 1
        },
        {
          label: "停用",
          value: 0
        }
      ],
      fileList: [],
      info: this.pageInfo || { name: "", code: "", fileId: "", status: 1 }
    };
  },
  computed: {
    isNew() {
      return this.dialogType === "New";
    }
  },
  methods: {
    handleAddPage() {
      addPage(this.info)
        .then(response => {
          this.$message({
            showClose: true,
            message: "新增页面成功！",
            type: "success"
          });
          this.$emit("closeToggle", true);
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    handleEditPage() {
      editPage(this.info)
        .then(response => {
          this.$message({
            showClose: true,
            message: "修改页面成功！",
            type: "success"
          });
          this.$emit("closeToggle", true);
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    cancel() {
      this.$emit("closeToggle", false);
    },
    uploadFileMethod(param) {
      const fileObject = param.file;
      const formData = new FormData();
      formData.append("file", fileObject);

      this.$store
        .dispatch("fileupload/uploadFile", { formData: formData })
        .then(fileId => {
          this.info.fileId = fileId;
          this.handleAddPage();
        })
        .catch(message => {
          console.log("message======================", message);
          this.$message.error("上传失败，请联系管理员");
        });
    },
    submitUpload() {
      if (this.isNew) {
        this.$refs.upload.submit();
      } else {
        this.handleEditPage();
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
      console.log(file.name);
    }
  }
};
</script>
